﻿<%@ Page Title="Promotion Engine -  Customer" Language="C#" MasterPageFile="~/Popup.Master"
    AutoEventWireup="true" CodeBehind="CustomerList.aspx.cs" Inherits="PromotionEngine.CustomerList" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <script type="text/javascript">
        var oCustomer = {};
        var isInsert = false;
        var grid;
        var keySearch;
        function Search() {
            keySearch = $("#KeySearch").val();
            grid.dataSource.read();
        }

        function onChange(arg) {
            var selectedRows = this.select();
            var selectedDataItems = [];
            for (var i = 0; i < selectedRows.length; i++) {
                var dataItem = this.dataItem(selectedRows[i]);
                selectedDataItems.push(dataItem);
            }
            if (selectedDataItems.length > 0) {
                parent.ChooseCustomer(selectedDataItems[0].CustomerID, selectedDataItems[0].CustomerName);
            }
        }
        $(document).ready(function () {
            setTimeout(function () { $("#KeySearch").focus(); }, 0);
            $("#KeySearch").keydown(function (event) {
                if (event.keyCode == 13) {
                    event.preventDefault();
                    Search();
                    return false;
                }
            });
            grid = $("#grid").kendoGrid({
                dataSource: {
                    transport: {
                        read: {
                            type: "GET",
                            url: "CustomerList.aspx/GetCustomerList",
                            contentType: "application/json; charset=utf-8",
                            dataType: "json"
                        },
                        parameterMap: function (options, operation) {
                            if (operation === "read") {
                                return { keySearch: kendo.stringify(keySearch) };
                            }
                        },
                        serverPaging: true,
                        serverSorting: true,
                        serverFiltering: true
                    },
                    type: "json", //Important!!!!
                    schema: {
                        data: function (response) {
                            if (response.d != null && response.d != '') {
                                var data = response.d;
                                return JSON.parse(data);
                            }
                        },
                        total: function (response) {
                            if (response.d != '') {
                                return JSON.parse(response.d).length;
                            }
                            else {
                                return 0;
                            }
                        },
                        model: {
                            id: "CustomerID",
                            fields: {
                                CustomerID: { type: "string" },
                                CustomerCode: { type: "string", validation: { required: true} },
                                CustomerName: { type: "string", validation: { required: true} },
                                Birthday: { type: "date", validation: { required: true} },
                                SpecialDay: { type: "date", validation: { required: true} },
                                CompanyCode: { type: "string" },
                                CreatedUserID: { type: "string" },
                                CreatedDate: { type: "date" },
                                BillingAddress: { type: "string", validation: { required: true} },
                                ShippingAddress: { type: "string", validation: { required: true} },
                                ContactPerson: { type: "string", validation: { required: true} },
                                PhoneNumber: { type: "string", validation: { required: true} },
                                Email: { type: "string", validation: { required: true} },
                                IsActive: { type: "boolean" }
                            }
                        }
                    }
                },
                change: onChange,
                sortable: true,
                pageable: {
                    pageSize: 20,
                    refresh: true
                },
                filterable: true,
                reorderable: true,
                resizable: true,
                height: 470,
                selectable: "row",
                batch: true,
                columns: [{ field: "CustomerID", title: "ID", width: 80, hidden: true },
                            { field: "CustomerCode", title: "Code", width: 140 },
                            { field: "CustomerName", title: "Name" },
                            { field: "ContactPerson", title: "Contact Person", width: 200 },
                            { field: "PhoneNumber", title: "Phone", width: 200 },
                            { field: "Email", title: "Email", width: 200 }
                            ]
                , editable: {
                    mode: "popup"
                },
                edit: function (e) {
                    var editWindow = e.container.data("kendoWindow");

                },
                remove: function (e) {
                },
                save: function (e) {
                }
            }).data("kendoGrid");
        });
    </script>
    <table style="width: 100%;" border="0">
        <tr>
            <td style="width: 85%;">
                <input type="text" class="k-input k-textbox" name="KeySearch" id="KeySearch" style="width: 100%;" />
            </td>
            <td align="right">
                <button type="button" id="btnSave" onclick="Search();" class="k-button k-button-icontext k-primary"
                    style="width: 150px;">
                    <span class="k-icon k-i-search"></span>Find</button>
            </td>
        </tr>
    </table>
    <div style="position: absolute; z-index: 0; margin-right: 8px; margin-top: 10px;">
        <div id="grid">
        </div>
    </div>
</asp:Content>
